<?php
$name = get_input ( "name", "" );
$description = get_input ( "description", "" );
$group_project = get_input ( "group_project", "" );
$semester = get_input ( "semester", "" );
$academic_year = get_input ( "academic_year", "" );
$group_guid = get_input ( "group_guid", "" );
$total_member = get_input ( "total_member", 0 );
$projectlistguid = get_input ( "projectlist_guid", 0 );
$is_error = false;

$waiting = get_input ( 'waiting' );

$group = get_entity ( $group_guid );

if ($group) {
	if (! $group->isMember ( elgg_get_logged_in_user_entity () )) {
		groups_join_group ( $group, elgg_get_logged_in_user_entity () );
	}
}

$listproj_url = elgg_get_site_url () . "submana/listpro/$group_guid";
if ($group_project != "" && $semester != "" && $academic_year != "" && $projectlistguid != 0) {
	if ($total_member > 0) {
		$members = array ();
		for($i = 0; $i < $total_member; $i ++) {
			// add student to project
			$student_id = get_input ( "member" . ($i + 1), $DEFAULT );
			
			if ($student_id != $DEFAULT) {
				if ($i != 0) {
					$student = get_user_by_username ( $student_id );
				} else {
					$student = elgg_get_logged_in_user_entity ();
				}
				if ($student) {
					
					foreach ( $members as $member ) {
						if ($member == $student->username) {
							$is_error = true;
							register_error ( "Đăng kí không thành công. Sinh viên không thể trùng nhau" );
							forward ( REFERER );
							exit ();
						}
					}
					if (! $is_error) {
						array_push ( $members, "$student->username" );
						$j ++;
					}
				} else {
					$is_error = true;
					register_error ( elgg_echo ( 'submana:project:noneuser', array (
							"$student_id" 
					) ) );
					forward ( REFERER );
					exit ();
				}
			} else {
			}
		}
		
		if (! $is_error) {
			
			$projectlists = elgg_get_entities ( array (
					'type' => "object",
					'subtype' => "projectlist",
					'container_guid' => $group_guid 
			) );
			
			if (! can_write_to_container ( elgg_get_logged_in_user_guid (), $group_guid )) {
				system_message ( "Bạn cần tham gia vào nhóm mới có thể đăng kí đề tài" );
				forward ( $listproj_url );
				exit ();
			}
			// Da co nhom dang ki nhung chua het slot
			$newproject = new ElggObject ();
			$newproject->subtype = "project";
			$newproject->title = $name;
			$newproject->description = $description;
			$newproject->max_group = 1;
			$newproject->current_group = 1;
			$newproject->group_project = $group_project;
			$newproject->max_student = $total_member;
			$newproject->min_student = $total_member;
			$newproject->deadline = date ( "d/m/Y", time () );
			
			$newproject->container_guid = $group_guid;
			$newproject->projectlist_guid = $projectlistguid;
			$newproject->access_id = ACCESS_LOGGED_IN;
			$newproject->semester = $semester;
			$newproject->academic_year = $academic_year;
			$newproject->students = $members;
			$newproject->priority = PRIORITY_LOW;
			$newproject->is_out_list = true;
			$newproject->save ();
			
			foreach ( $members as $student_username ) {
				$student = get_user_by_username ( $student_username );
				if ($student->classes) {
					$student->classes .= ",$group_project`$semester`$academic_year";
				} else {
					$student->classes .= "$group_project`$semester`$academic_year";
				}
				$student->save ();
			}
			
			system_message ( "Đăng kí thành công" );
			forward ( $listproj_url );
			exit ();
		} else {
			register_error ( "Đăng kí không thành công, vui lòng đăng kí lại" );
		}
	} else {
		register_error ( "Số thành viên không hợp lệ, vui lòng đăng kí lại" );
	}
} else {
	register_error ( "Thông tin chưa chính xác, group_project = $group_project, semester = $semester,
	accademic = $academic_year,  projectlist = $projectlistguid, group = $group_guid
	, vui lòng đăng kí lại" );
}
forward ( REFERER );
